/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.admin;

import java.util.ArrayList;
import java.util.List;

import net.groupme.controller.model.HtmlFormattedElement;
import net.groupme.gui.constants.VIEW_VOCABULARY;
import net.groupme.gui.rendering.TagRenderer;
import net.groupme.search.SEARCH_CONFIGURATION;
import net.groupme.search.SearchManager;
import net.groupme.search.strategy.model.WeightedTag;
import net.groupme.user.UserManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * created on Aug 2, 2007
 * 
 * This class provides some administration functionality like removing users,
 * editing groups, tags, ..., recomputing cached tag clouds,...
 * 
 * @author Fabian Abel, <a href="mailto:abel@l3s.de">abel@l3s.de</a>
 * @author last edited by: $Author: fabian $
 * 
 * @version $Revision: 1.1 $ $Date: 2007-08-02 16:45:11 $
 */
public class AdminManager implements ApplicationContextAware{

	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());
	
	private static List<String> ADMIN_USERNAMES = new ArrayList<String>();
	static{
		String[] admins = AdminPropertyManager.getStringArray("admin.users");
		for(int i=0; i<admins.length; i++){
			ADMIN_USERNAMES.add(admins[i]);
		}
	}
	
	/** the application context */
	private ApplicationContext applicationContext = null;
	
	/**
	 * Creates a new AdminManager instance. 
	 */
	public AdminManager() {
		super();
	}

	/**
	 * Set the ApplicationContext that this object runs in. 
	 * @param applicationContext the current {@link ApplicationContext} 
	 */
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}
	
	/**
	 * Returns <code>true</code> if the user is lagged in with an administration account.
	 * @return <code>true</code> if the user is logged in with an administration account, otherwise <code>false</code>
	 */
	public boolean isLoggedIn(){
		UserManager userManager = (UserManager) applicationContext.getBean(VIEW_VOCABULARY.USER_MANGER, UserManager.class);
		logger.info("Logged in?: " + userManager.isLoggedIn());
		if(userManager.isLoggedIn()){
			logger.info("Logged in as: " + userManager.getUser().getUserName());
		}
		return userManager.isLoggedIn() && ADMIN_USERNAMES.contains(userManager.getUser().getUserName());
	}
	
	/**
	 * Recomputes the overall tag cloud and returns it as {@link HtmlFormattedElement}.
	 * @return the recomputed tag cloud as {@link HtmlFormattedElement}
	 */
	public HtmlFormattedElement computeOverallTagCloud(){
		SearchManager searchManager =(SearchManager) applicationContext.getBean(VIEW_VOCABULARY.GROUPME_SEARCH_MANGER, SearchManager.class);
		List<WeightedTag> weightedTags = searchManager.computeTagCloud(null, null, null, SEARCH_CONFIGURATION.TAG_CLOUD_BIG_MAX_NUMBER_OF_TAGS);
		HtmlFormattedElement tagCloud = new HtmlFormattedElement(null, new TagRenderer().renderTagCloud(weightedTags));
		searchManager.setCachedTagCloud(tagCloud);
		return tagCloud;
	}
}
